<?php
/**
 * @return
 * @author 朱乐斌
 * @date
 */

namespace app\api\controller;

use app\api\model\ResumeModel;
use app\api\model\UserGxModel;
use app\api\model\WorkModel;
use app\api\validate\WorkValidate;
use think\Request;

class Work extends BaseController
{

    // 删除工作经历
    public function deleteWork(Request $request)
    {
        // 接收删除工作经历的id
        $id = $this->request->post('id/d');
        try {
            // 获取用户id
            $user = UserGxModel::where('phone',$request->phone)->field('id,name,phone,type')->find();
            // 查询出简历id
            $resume = (new \app\api\model\ResumeModel)->where('user_id','=',$user['id'])->find();
            // 查询关联的工作经历并且删除
            $work = (new \app\api\model\WorkModel)->where('id',$id)->field('id,user_id')->find();
            // 判断是为用户的工作经历
            if ($work['user_id'] !== $resume['id']){
                return errMsg('数据错误');
            }
            $work->delete();
            return succMsg((array)'删除成功');
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }

    // 查看用户工作经历
    public function getWorkList(Request $request)
    {
        try {
            // 获取用户id
            $user = UserGxModel::where('phone',$request->phone)->field('id,name,phone,type')->find();
            // 查询用户简历 ps：工作经历表关联的了简历表，没用关联用户
            if ($user['type'] == 2) return errMsg('您是企业用户无需获取工作经历列表');
            $resume = (new \app\api\model\ResumeModel)->where('user_id','=',$user['id'])->find();
            if (empty($resume)) return errMsg('请先填写简历');
            // 使用简历id查询出工作经历 ps：user_id应该是resume_id,建表建错了
            $work = (new \app\api\model\WorkModel)->where('user_id',$resume['id'])->select()->toArray();
            if (empty($work)) return errMsg('暂无工作经历');
            return succMsg($work);
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }

    // 添加工作经历
    public function addWork(Request $request)
    {
        // 接收工作经历的数据
        $data = $this->request->post();
        try {
            // 获取用户id
            $user = UserGxModel::where('phone',$request->phone)->field('id,name,phone,type')->find();
            if ($user['type'] == 2) return errMsg('您是企业用户无需添加工作经历');
            // 验证数据是否正确
            validate(WorkValidate::class)->scene('addWork')->check($data);
            // 查出简历id
            $resume = ResumeModel::where('user_id', '=',$user['id'])->field('id,user_id')->find();
            // 检查工作经历是否重复
            $existingWork = WorkModel::where([
                'user_id' => $resume['id'],
                'name' => $data['name'],
                'position' => $data['position'],
                'content' => $data['content'],
            ])->find();
            if ($existingWork) return errMsg('工作经历已存在');
            // 如果不重复添加数据
            $wordData = [
                'user_id' => $resume['id'],
                'name' => $data['name'],
                'position' => $data['position'],
                'content' => $data['content'],
                'create_time' => date('Y-m-d H:i:s',time()),
                'resignation_time' => date('Y-m-d H:i:s',time())
            ];
            // 添加工作经历
            $work = (new \app\api\model\WorkModel)->save($wordData);
            if ($work) return json([
               'msg' => '添加成功',
               'code' => 1,
               'data' => ''
            ]);
            return errMsg('添加失败');
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }

    // 查看工作经历详细
    public function detailWork(Request $request)
    {
        $user = UserGxModel::where('phone','=',$request->phone)->find();
        if ($user['type'] == 2) $this->error('失败哦');
        // 接收数据
        $id = $this->request->post('id/d');
        $work = WorkModel::where('id',$id)->find();
        if ($work == null) $this->error('失败哦');
        $this->success($work);
    }

}